﻿/**
 * p2p首页中"图片轮播"功能
 */
var p2p = p2p || {};

   /**
    * 图片轮播
    * @param   [ string ]   ImgName
    * @param   [ string ]   HandName
    */
p2p.SwapImg = (function () {
    
    function _SwapImage(ImgName, HandName) {
        this.ImgControl = $(ImgName);
        this.HandControl = $(HandName);
        this.Time1 = null;
        this.Time1Date = 3000;
        this.Time2 = null;
        this.Time2Date = 80;
        this.i = 0;

        this.init();
    };

    _SwapImage.prototype = {
        init: function () {
            var me = this;
            this.HandControl.each(function (idex) {
                $(this).bind("click", function () {
                    me.myImgClick.call(me, idex);
                });
            });

            this.Time1 = setInterval(function () {
                me.myfunction.call(me);
            }, this.Time1Date);
        },
        myImgClick: function (idex) {
            var me = this;
            clearInterval(this.Time1);
            clearInterval(this.Time2);
            this.ImgControl[this.i].style.display = "none";
            this.ImgControl[this.i].style.opacity = "0";
            this.HandControl[this.i].className = "";
            this.i = idex;
            this.myfunction3.call(me, idex);
        },
        myfunction: function () {
            var me = this;
            this.ImgControl[me.i].style.display = "none";
            this.ImgControl[me.i].style.opacity = "0";
            this.HandControl[me.i].className = "";
            this.i++;
            if (me.i >= me.ImgControl.length) me.i = 0;
            this.myfunction3.call(me, this.i);
        },
        myfunction3: function (index) {
            var me = this;
            for (var idex = 0; idex < this.ImgControl.length; idex++) {
                if (idex == index) {
                    me.ImgControl[idex].style.display = "list-item";
                    me.ImgControl[idex].style.opacity = "0";
                    me.HandControl[idex].className = "current";
                    clearInterval(me.Time1);
                    me.Time2 = setInterval(function () {
                        me.myfunction1.call(me);
                    }, me.Time2Date);
                    break;
                }
            }
        },
        myfunction1: function () {
            var me = this;
            var s1 = me.ImgControl[me.i].style.opacity;
            s1 = parseFloat(s1) + parseFloat("0.1");
            if (s1 > 1) {
                clearInterval(me.Time2);
                this.Time1 = setInterval(function () {
                    me.myfunction.call(me);
                }, this.Time1Date);
            }
            this.ImgControl[me.i].style.opacity = s1;
        }
    };
    return {
        swap: function (ImgName, HandName) {
            new _SwapImage(ImgName, HandName);
        }
    }
})();

